# include"head.h"
// 保存接收的文件偏移量到文件中
void save_received_offset(const char *path,const char *file_name, off_t offset) {
    char offset_file[256];
    
    snprintf(offset_file, sizeof(offset_file), "%s.offset", path);
    FILE *fp = fopen(offset_file, "w");
    if (fp) {
        fprintf(fp, "%ld\n", offset);
        fclose(fp);
    }
}

// 从文件中读取已接收的文件偏移量
off_t get_received_offset(const char *path,const char *file_name) {
    char offset_file[256];
    snprintf(offset_file, sizeof(offset_file), "%s.offset", path);
    FILE *fp = fopen(offset_file, "r");
    off_t offset = 0;
    if (fp) {
        fscanf(fp, "%ld", &offset);
        fclose(fp);
    }
    return offset;
}
//创建目录
bool creat_dir(const char*path){
 // mkdir(,066);
    return false;
}
bool my_puts(sendMsg msg, client_status *user,int net_fd){

    char path[100];
    bzero(path,sizeof(path));

    char full_path[1024]={0};
    //获取当前工作目录
    strcpy(full_path,user->root_path);
   // getcwd(full_path,sizeof(full_path));
    strcat(path,full_path);
    printf("path1:%s\n",path);
    strcat(path,msg.msg);
    strcat(path,"/");
    printf("path2:%s\n",path);
    // 接收文件大小
    off_t file_size;
    int ret=recv(net_fd, &file_size, sizeof(off_t), MSG_WAITALL);
    ERROR_CHECK(ret,-1,"ret_file_size");
    printf("file_size: %ld %d\n", file_size,ret);


    // 先读取文件名字长度
    // 再根据文件名字长度: 读取文件名
    int file_name_len ;
    int len_ret=recv(net_fd, &file_name_len, sizeof(int),  MSG_WAITALL);
    ERROR_CHECK(len_ret,-1,"len_ret");
    char file_name[256] = {0};
    int ret_recv=recv(net_fd, file_name, file_name_len,  MSG_WAITALL);
    ERROR_CHECK(ret_recv,-1,"ret_recv");
    printf("接收到的文件信息：%d %s\n",len_ret,file_name);
 
    strcat(path,file_name);
   // strcat(path,"/");
    printf("path3:%s\n",path);
    int file_fd = open(path, O_RDWR|O_CREAT, 0600);
    ERROR_CHECK(file_fd,-1,"file_fd");
    
    //创建目录
    char path_dir[2000];
    bzero(path_dir,sizeof(path_dir));
    strcat(path_dir,full_path);
    strcat(path_dir,msg.msg);
    /*if(file_fd==-1){
        if(creat_dir(path_dir)){
            //创建成功
        }else{
            //退出 发送
            return false;
        }
    }*/
    // 文件调整需要的大小
    int ret_f=ftruncate(file_fd, file_size);
    ERROR_CHECK(ret_f,-1,"ret_f");

     off_t received_size = get_received_offset(path,file_name);
  // printf("偏移量 ：%ld\n",received_size);
     int ret_send_size=send(net_fd,&received_size,sizeof(off_t),0);
    ERROR_CHECK(ret_send_size,-1,"ret_send_size");
   
    char *p = (char *)mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, file_fd, 0);
      if (p == MAP_FAILED) { // 错误处理
        perror("mmap");
        close(file_fd);
        return false;
    }
       
    ssize_t ret_recv1;
    while (received_size < file_size) {
        ret_recv1 = recv(net_fd, p + received_size, file_size - received_size, 0);
        if (ret_recv1 <= 0) {
            if (errno == EINTR || errno == EAGAIN) {
                continue; // 如果发生了中断或资源临时不可用，继续接收
            } else {
                perror("recv file data");
                munmap(p, file_size);
                close(file_fd);
                save_received_offset(path,file_name, received_size);
                return false;
            }
        
        }

        printf("recv_bytes:%ld\n",ret_recv1);
        received_size += ret_recv1;
        sleep(3);
    }
  // int ret_recv1= recv(socket_fd, p, file_size, MSG_WAITALL);
   // ERROR_CHECK(ret_recv1,-1,"ret_recv"); 
   munmap(p, file_size);

    close(file_fd);
    close(net_fd);
    save_received_offset(path,file_name, received_size);
    return 0;



}

